<!DOCTYPE html>

<head>
    <meta charset="utf-8">
    <title>门店分润管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="../../../js/vue.js"></script>
    <script type="text/javascript" src="../../../js/jquery-2.1.4.js"></script>
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>


<!-- 每一行工具条 -->
<script type="text/html" id="barDemo">
    {{#  if(update){ }}
    <button type="button" class="layui-btn layui-btn-xs layui-btn-radius layui-btn-warm" lay-event="singleUpdate">
        <i class="layui-icon">&#xe642;</i> 全部结算
    </button>
    {{#  } }}


    <button class="layui-btn layui-btn-primary layui-btn-radius layui-btn-xs" lay-event="detail">查看</button>

</script>

<!---------------------------------------------条件搜索框------------------------------------------->
<div class="demoTable" style="margin-top: 10px;margin-left: 10px">
    <form id="serachForm" class="layui-form">
        商家名：
        <div class="layui-inline" style="width: 200px">
            <input class="layui-input" name="businessName" id="searchName" autocomplete="off">
        </div>
        电话：
        <div class="layui-inline" style="width: 200px">
            <input class="layui-input" name="businessTel" id="searchTel" autocomplete="off">
        </div>


        <div class="layui-inline" style="margin-left: 10px">
            <!--这个地方的不能直接使用button标签，使用button标签会重复提交数据-->
            <input type="button" class="layui-btn" data-type="reload" value="搜索">
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </form>
</div>
<!-----------------------------------------表格内容--------------------------------------------->
<div id="content">
    <table class="layui-table"
           lay-data="{url:'/houtai/finance/getBusinessPayFinances',page:true,limit:10,limits:[5,10],toolbar:'#headbar',
                     height:'full-100',totalRow:true,loading:true,title:'门店消费分润列表(以门店为单位)',
                     //把返回的数据result转为换layUI需要的格式，此处返回的是mybatisPlus的IPage对象，具体返回值自己设定
                     parseData: function(res){ //res 即为原始返回的数据
                        console.log(res);
                        return {
                            'code': res.code, //解析接口状态
                            'msg': res.message, //解析提示文本
                            'count': res.obj.totalSize, //解析数据长度
                            'data': res.li //解析数据列表
                          };
                       },
                //给表格设置一个ID
               id:'test'}"
           lay-filter="filter">
        <thead>
        <tr>
            <th lay-data="{checkbox:true}"></th><!--开起全选框-->
            <th lay-data="{field:'businessId', sort: true,hide:true}">商家ID</th><!--field：实体类属性，sort：排序-->
            <th lay-data="{field:'businessName',totalRowText:'合计'}">商家名</th>
            <th lay-data="{field:'businessTel'}">电话</th>
            <th lay-data="{field:'account',hide:true}">账号</th>
            <th lay-data="{field:'unShareMoney',totalRow: true,templet:function(d){
                return Number(d.unShareMoney).toFixed(2);
            }}">未分润金额</th>
            <th lay-data="{field:'totalMoney',totalRow: true,templet:function(d){
                return Number(d.totalMoney).toFixed(2);
            }}">总金额</th>
            <th v-if="isShow" lay-data="{field:'opeartion', toolbar: '#barDemo',width : 200}">操作</th>

        </tr>
        </thead>
    </table>
</div>


<script src="../../../layuiadmin/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->


<script>

    var update= false;
    var isShow=false;

    //利用Vue判断是否具备每一行的删除或者修改权限
    var vue = new Vue({
        el:"#content",
        data:{
            buttons:[]

        },
        created:function(){

            $.ajax({
                url : "/houtai/page/finance/businesspay/sharewithbusiness/list",
                type : "get",
                dataType : "json",
                success : function(data) {
                    if(data.message=="success"){
                    }else if(data.message == "premissionException"){
                        location.href = "/houtai/page/exception/NoPremission.html";
                    }else{
                        location.href = "/houtai/page/exception/500.html";
                    }
                }
            });
        },

        methods: {

        },
        computed:{
            isShow:function () {
                var _this = this;
                var delbtn=false;
                var updatebtn=false;
                //获取当前网页地址
                var url = location.pathname;
                $.ajax({
                    url: "/houtai/adminuser/buttons",
                    type: "get",
                    async: false,
                    data: {
                        url: url
                    },
                    dataType: "json",
                    success: function (data) {

                        _this.buttons = data.li;

                        for(var i=0;i<_this.buttons.length;i++){
                            if(_this.buttons[i].menuUrl.indexOf("update")!=-1){
                                update = true;
                                isShow = true;
                                updatebtn =  true;
                                break;
                            }
                        }

                    }
                });


                if(updatebtn){
                    return true;
                }else{
                    return false;
                }

            }
        }

    })

    layui.use(['table','jquery','laypage','layer','form'], function(){
        var table = layui.table;
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;

        //监听顶部工具栏事件

        var index = null;
        //监听每一行工具条
        table.on('tool(filter)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据

            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent === 'detail'){//查看
                $.ajax({
                    url:"/houtai/page/finance/businesspay/qureyBusinessPayById",
                    data:{businessId:data.businessId},
                    type:"get",
                    dataType:"json",
                    success:function (data) {
                        if(data.message == "success"){
                            location.href= "/houtai/page/finance/businesspay/listforadmin.html";
                        }else if(data.message == "premissionException"){
                            location.href = "/houtai/page/exception/NoPremission.html";
                        }else if(data.message == "serverException"){
                            location.href = "/houtai/page/exception/500.html";
                        }else{
                            layer.msg("查询失败", {icon: 5});
                        }
                    }
                });

            } else if(layEvent === 'singleUpdate'){ //编辑
                if(data.unShareMoney==0){
                    layer.msg("已全部结算", {icon: 5});
                }else {
                    layer.confirm('确认结算', function(index) {
                        layer.close(index);
                        $.ajax({
                            url: "/houtai/page/finance/businesspay/updateAllByBusiId",
                            data: {businessId: data.businessId},
                            type: "post",
                            dataType: "json",
                            success: function (data) {
                                if (data.message == "success") {
                                    layer.msg("结算成功", {icon: 6});
                                    //重载表格
                                    $(".layui-laypage-btn").click();
                                } else if (data.message == "premissionException") {
                                    location.href = "/houtai/page/exception/NoPremission.html";
                                } else if (data.message == "serverException") {
                                    location.href = "/houtai/page/exception/500.html";
                                } else {
                                    layer.msg("结算失败", {icon: 5});
                                }
                            }
                        });
                    });
                }
            }
        });


        //设置搜索框查询条件
        var $ = layui.$, active = {
            reload: function(){
                var demoReload = $('#demoReload');
                var str = $("#serachForm").serialize();
                //执行重载
                table.reload('test', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        businessName:$("#searchName").val(),
                        businessTel:$("#searchTel").val()
                    }
                }, 'data');
            }
        };

        //监听搜索框提交按钮
        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

</script>

</body>
</html>